home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / bib-mode.el < prev    next >
Encoding:
Text File  |  1995-04-17  |  7.0 KB  |  241 lines

  1. ;;; bib-mode.el --- bib-mode, major mode for editing bib files.
  2. ;; Keywords: bib, wp
  3.  
  4. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.  
  23. ;; Bib-Mode
  24. ;;   GNU Emacs code to help maintain databases compatible with (troff)
  25. ;;   refer and lookbib.  The file bib-file should be set to your 
  26. ;;   bibliography file.  Keys are automagically inserted as you type,
  27. ;;   and appropriate keys are presented for various kinds of entries.
  28.  
  29. (provide 'bib-mode)
  30.  
  31. (defvar bib-file "~/my-bibliography.bib" 
  32.    "Default name of file used by addbib")
  33.  
  34. (defvar unread-bib-file "~/to-be-read.bib"
  35.    "Default name of file used by unread-bib in bib-mode")
  36.  
  37. (defvar bib-mode-map (copy-keymap text-mode-map))
  38. (define-key bib-mode-map "\C-M" 'return-key-bib)
  39. (define-key bib-mode-map "\C-c\C-u" 'unread-bib)
  40. (define-key bib-mode-map "\C-c\C-@" 'mark-bib)
  41. (define-key bib-mode-map "\e`" 'abbrev-mode)
  42. (defvar bib-mode-abbrev-table nil
  43.    "Abbrev table used in bib mode")
  44.  
  45. (defun addbib ()
  46.    "Set up editor to add to troff bibliography file specified 
  47. by global variable bib-file.  See description of bib-mode."
  48.    (interactive)
  49.    (find-file bib-file)
  50.    (goto-char (point-max))
  51.    (bib-mode)
  52.    )
  53.    
  54. (defun bib-mode ()
  55.    "Mode for editing lookbib style bibliographies.  
  56. Hit RETURN to get next % field key.
  57. If you want to ignore this field, just hit RETURN again.
  58. Use text-mode to turn off.  
  59.  
  60.  journal papers:                    A* T D J V N P K W X
  61.  articles in books & proceedings:   A* T D B E* I C P K W X 
  62.  tech reports:                      A* T D R I C K W X
  63.  books:                             A* T D I C K W X
  64.  
  65. Fields:
  66.  
  67. A uthor        T itle        D ate          J ournal
  68. V olume        N umber        P age        K eywords
  69. B in book or proceedings    E ditor        C ity & state
  70. I nstitution, school, or publisher
  71. R eport number or 'phd thesis' or 'masters thesis' or 'draft' or 
  72.      'unnumbered' or 'unpublished'
  73. W here can be found locally (login name, or ailib, etc.)
  74. X comments (not used in indexing)
  75.  
  76. \\[unread-bib] appends current entry to a different file (for 
  77. example, a file of papers to be read in the future), given by
  78. the value of the variable unread-bib-file.
  79. \\[mark-bib] marks current or previous entry.
  80. Abbreviations are saved in bib-mode-abbrev-table.
  81. Hook can be stored in bib-mode-hook.
  82. Field keys given by variable bib-assoc.
  83.  
  84. Commands:
  85. \\{bib-mode-map}
  86. "
  87.    (interactive)
  88.    (text-mode)
  89.    (use-local-map bib-mode-map)
  90.    (setq mode-name "Bib")
  91.    (setq major-mode 'bib-mode)
  92.    (define-abbrev-table 'bib-mode-abbrev-table ())
  93.    (setq local-abbrev-table bib-mode-abbrev-table)
  94.    (abbrev-mode 1)
  95.    (run-hooks 'bib-mode-hook)
  96.    )
  97.  
  98. (defconst bib-assoc '(
  99.            (" *$" . "%A ")
  100.            ("%A ." . "%A ")
  101.            ("%A $" . "%T ")
  102.            ("%T " . "%D ")
  103.            ("%D " . "%J ")
  104.            ("%J ." . "%V ")
  105.            ("%V " . "%N ")
  106.            ("%N " . "%P ")
  107.            ("%P " . "%K ")
  108.            ("%K " . "%W ")
  109.            ("%W " . "%X ")
  110.            ("%X " . "")
  111.            ("%J $" . "%B ")
  112.            ("%B ." . "%E ")
  113.            ("%E ." . "%E ")
  114.            ("%E $" . "%I ")
  115.            ("%I " . "%C ")
  116.            ("%C " . "%P ")
  117.            ("%B $" . "%R ")
  118.            ("%R " . "%I ")
  119.            )
  120.            
  121. "Describes bibliographic database format.  A line beginning with
  122. the car of an entry is followed by one beginning with the cdr.
  123. ")
  124.  
  125. (defun bib-find-key (slots)
  126.    (cond
  127.       ((null slots)
  128.      (if (bobp)
  129.         ""
  130.         (progn (previous-line 1) (bib-find-key bib-assoc))))
  131.       ((looking-at (car (car slots)))
  132.      (cdr (car slots)))
  133.       (t (bib-find-key (cdr slots)))
  134.       ))
  135.  
  136.  
  137. (defvar bib-auto-capitalize t 
  138. "*True to automatically capitalize appropriate
  139. fields in bib-mode")
  140.  
  141. (defconst bib-capitalized-fields "%[AETCBIJR]")
  142.  
  143. (defun return-key-bib ()
  144.   "Magic when user hits return, used by bib-mode"
  145.   (interactive)
  146.   (if (eolp)
  147.     (let (empty new-key beg-current end-current)
  148.       (beginning-of-line)
  149.       (setq empty (looking-at "%. $"))
  150.       (if (not empty)
  151.     (progn
  152.       (end-of-line)
  153.       (newline)
  154.       (forward-line -1)
  155.       ))
  156.       (end-of-line)
  157.       (setq end-current (point))
  158.       (beginning-of-line)
  159.       (setq beg-current (point))
  160.       (setq new-key (bib-find-key bib-assoc))
  161.       (if (and (not empty) bib-auto-capitalize
  162.         (looking-at bib-capitalized-fields))
  163.     (save-excursion
  164.       (capitalize-title-region (+ (point) 3) end-current)))
  165.       (goto-char beg-current)
  166.       (if empty
  167.     (kill-line nil)
  168.     (forward-line 1)
  169.     )
  170.       (insert-string new-key))
  171.     (newline)))
  172.  
  173. (defun mark-bib ()
  174.    "set mark at beginning of current or previous bib entry, point at end"
  175.    (interactive)
  176.    (beginning-of-line nil)
  177.    (if (looking-at "^ *$") (re-search-backward "[^ \n]" nil 2))
  178.    (re-search-backward "^ *$" nil 2)
  179.    (re-search-forward "^%")
  180.    (beginning-of-line nil)
  181.    (push-mark (point))
  182.    (re-search-forward "^ *$" nil 2)
  183.    (next-line 1)
  184.    (beginning-of-line nil)
  185.    )
  186.  
  187. (defun unread-bib ()
  188.    "append current or previous entry to file of unread papers
  189. named by variable unread-bib-file"
  190.    (interactive)
  191.    (mark-bib)
  192.    (if (get-file-buffer unread-bib-file)
  193.       (append-to-buffer (get-file-buffer unread-bib-file) (mark) (point))
  194.       (append-to-file (mark) (point) unread-bib-file)
  195.       )
  196.    )
  197.  
  198.  
  199. (defvar capitalize-title-stop-words
  200.    (concat
  201.       "the\\|and\\|of\\|is\\|a\\|an\\|of\\|for\\|in\\|to\\|in\\|on\\|at\\|"
  202.       "by\\|with\\|that\\|its")
  203.    "Words not to be capitialized in a title (unless they are the first
  204. word in the title)")
  205.  
  206. (defvar capitalize-title-stop-regexp
  207.    (concat "\\(" capitalize-title-stop-words "\\)\\(\\b\\|'\\)"))
  208.  
  209. (defun capitalize-title-region (begin end)
  210.    "Like capitalize-region, but don't capitalize stop words, except the first"
  211.    (interactive "r")
  212.    (let ((case-fold-search nil) (orig-syntax-table (syntax-table)))
  213.       (unwind-protect
  214.      (save-restriction
  215.         (set-syntax-table text-mode-syntax-table)
  216.         (narrow-to-region begin end)
  217.         (goto-char (point-min))
  218.         (if (looking-at "[A-Z][a-z]*[A-Z]")
  219.            (forward-word 1)
  220.            (capitalize-word 1))
  221.         (while (re-search-forward "\\<" nil t)
  222.            (if (looking-at "[A-Z][a-z]*[A-Z]")
  223.           (forward-word 1)
  224.           (if (let ((case-fold-search t))
  225.              (looking-at capitalize-title-stop-regexp))
  226.              (downcase-word 1)
  227.              (capitalize-word 1)))
  228.            ))
  229.      (set-syntax-table orig-syntax-table))))
  230.  
  231.  
  232. (defun capitalize-title (s)
  233.    "Like capitalize, but don't capitalize stop words, except the first"
  234.    (save-excursion
  235.       (set-buffer (get-buffer-create "$$$Scratch$$$"))
  236.       (erase-buffer)
  237.       (insert s)
  238.       (capitalize-title-region (point-min) (point-max))
  239.       (buffer-string)))
  240.  
  241.